﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using diploma.core.data;
using diploma.core.data.objects;
using diploma.core.data.subobjects;
using diploma.core.localize;
using diploma.core;

namespace diploma.test.GUI
{
    public partial class AdminForm : Form, ILocalizable
    {
        private Dictionary<DataObjectTypes, DataList<diploma.core.data.IDataObject>> dataObjectsCollections;
        AbstractLocalizator localizator;
        public AdminForm(AbstractLocalizator _localizator, params DataList<diploma.core.data.IDataObject>[] _dataObjectsCollections)
        {
            InitializeComponent();
            localizator = _localizator;
            dataObjectsCollections = new Dictionary<DataObjectTypes, DataList<diploma.core.data.IDataObject>>();
            foreach (DataList<diploma.core.data.IDataObject> _dataObjects in _dataObjectsCollections)
            {
                dataObjectsCollections.Add(_dataObjects.ObjectsType, _dataObjects);
            }
            fillQuestions();
            fillTerms();
            fillObjects();
            Localize();
        }

        private void fillQuestions()
        {
            DataList<diploma.core.data.IDataObject> questions = dataObjectsCollections[DataObjectTypes.Question];
            foreach (Question question in questions)
            {
                dgvQuestions.Rows.Add(new object[] { question.Id, question.Content });
            }
        }

        private void fillObjects()
        {
            DataList<diploma.core.data.IDataObject> objects = dataObjectsCollections[DataObjectTypes.Property];
            foreach (Property property in objects)
            {
                dgvObjects.Rows.Add(new object[] { property.Id, property.Title });
            }
        }

        private void fillTerms()
        {
            DataList<diploma.core.data.IDataObject> terms = dataObjectsCollections[DataObjectTypes.Term];
            foreach (Term term in terms)
            {
                dgvTerms.Rows.Add(new object[] { term.Id, term.Title });
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            string q = "";
            if (InputBox(localizator.GetLocolizeWord("add_new_question"), localizator.GetLocolizeWord("enter_new_question"), ref q) == DialogResult.OK)
            {
                dataObjectsCollections[DataObjectTypes.Question].Add(new Question(-1, q));
                Question newQ = dataObjectsCollections[DataObjectTypes.Question][dataObjectsCollections[DataObjectTypes.Question].Count - 1] as Question;
                dgvQuestions.Rows.Add(new object[] { newQ.Id, newQ.Content });
            }
        }


        #region marazm
        public static DialogResult InputBox(string title, string promptText, ref string value)
        {
            Form form = new Form();
            Label label = new Label();
            TextBox textBox = new TextBox();
            Button buttonOk = new Button();
            Button buttonCancel = new Button();

            form.Text = title;
            label.Text = promptText;
            textBox.Text = value;

            buttonOk.Text = "OK";
            buttonCancel.Text = "Cancel";
            buttonOk.DialogResult = DialogResult.OK;
            buttonCancel.DialogResult = DialogResult.Cancel;

            label.SetBounds(9, 20, 372, 13);
            textBox.SetBounds(12, 36, 372, 20);
            buttonOk.SetBounds(228, 72, 75, 23);
            buttonCancel.SetBounds(309, 72, 75, 23);

            label.AutoSize = true;
            textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
            buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

            form.ClientSize = new Size(396, 107);
            form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
            form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
            form.FormBorderStyle = FormBorderStyle.FixedDialog;
            form.StartPosition = FormStartPosition.CenterScreen;
            form.MinimizeBox = false;
            form.MaximizeBox = false;
            form.AcceptButton = buttonOk;
            form.CancelButton = buttonCancel;

            DialogResult dialogResult = form.ShowDialog();
            value = textBox.Text;
            return dialogResult;
        }
        #endregion

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(localizator.GetLocolizeWord("want_delete_item"), localizator.GetLocolizeWord("delete_question"), MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                long id = Convert.ToInt64(dgvQuestions.SelectedRows[0].Cells[0].Value);
                dataObjectsCollections[DataObjectTypes.Question].Delete(id);
                dgvQuestions.Rows.Remove(dgvQuestions.SelectedRows[0]);
            }
        }

  

        private void modifyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string q = dgvQuestions.SelectedRows[0].Cells[1].Value.ToString();
            if (InputBox(localizator.GetLocolizeWord("update_question"), localizator.GetLocolizeWord("question"), ref q) == DialogResult.OK)
            {
                long id = Convert.ToInt64(dgvQuestions.SelectedRows[0].Cells[0].Value);
                dataObjectsCollections[DataObjectTypes.Question].Update(dgvQuestions.SelectedRows[0].Index, new Question(id, q));
                dgvQuestions.SelectedRows[0].Cells[1].Value = q;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
              


        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string q = "";
            if (InputBox(localizator.GetLocolizeWord("add_new_question_term"), localizator.GetLocolizeWord("term"), ref q) == DialogResult.OK)
            {
                dataObjectsCollections[DataObjectTypes.Term].Add(new Term(-1, q));
                Term newQ = dataObjectsCollections[DataObjectTypes.Term][dataObjectsCollections[DataObjectTypes.Term].Count - 1] as Term;
                dgvTerms.Rows.Add(new object[] { newQ.Id, newQ.Title });
            }
        }

        
        private void modifyToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            string q = dgvTerms.SelectedRows[0].Cells[1].Value.ToString();
            if (InputBox(localizator.GetLocolizeWord("update_term"), localizator.GetLocolizeWord("term"), ref q) == DialogResult.OK)
            {
                long id = Convert.ToInt64(dgvTerms.SelectedRows[0].Cells[0].Value);
                dataObjectsCollections[DataObjectTypes.Term].Update(dgvTerms.SelectedRows[0].Index, new Term(id, q));
                dgvTerms.SelectedRows[0].Cells[1].Value = q;
            }
        }

        

        private void delteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(localizator.GetLocolizeWord("want_delete_item"), localizator.GetLocolizeWord("delete_term"), MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                long id = Convert.ToInt64(dgvTerms.SelectedRows[0].Cells[0].Value);
                dataObjectsCollections[DataObjectTypes.Term].Delete(id);
                dgvTerms.Rows.Remove(dgvTerms.SelectedRows[0]);
            }
        }

        private void adToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string tv = "";
            if (InputBox(localizator.GetLocolizeWord("add_term_variant"), localizator.GetLocolizeWord("term_variant"), ref tv) == DialogResult.OK)
            {
                if (dgvTerms.SelectedRows.Count > 0)
                {
                    Term t = (dataObjectsCollections[DataObjectTypes.Term][dgvTerms.SelectedRows[0].Index] as Term);
                    t.Variants.Add(tv);
                    lbTermVariants.Items.Add(t.Variants[t.Variants.Count - 1]);
                    dataObjectsCollections[DataObjectTypes.Term].Update(dgvTerms.SelectedRows[0].Index, t);
                }
            }
        }

        private void dgvTerms_Click(object sender, EventArgs e)
        {
            if (dgvTerms.SelectedRows.Count > 0)
            {
                lbTermVariants.Items.Clear();
                Term t = dataObjectsCollections[DataObjectTypes.Term][dgvTerms.SelectedRows[0].Index] as Term;
                foreach (string var in t.Variants)
                {
                    lbTermVariants.Items.Add(var);
                }
            }
        }

        

        private void dToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string tv = Convert.ToString(lbTermVariants.SelectedItem);
            if (InputBox(localizator.GetLocolizeWord("modify_term_variant"), localizator.GetLocolizeWord("term_variant"), ref tv) == DialogResult.OK)
            {
                if (dgvTerms.SelectedRows.Count > 0)
                {
                    Term t = (dataObjectsCollections[DataObjectTypes.Term][dgvTerms.SelectedRows[0].Index] as Term);
                    t.Variants[lbTermVariants.SelectedIndex] = tv;
                    lbTermVariants.Items[lbTermVariants.SelectedIndex] = tv;
                    dataObjectsCollections[DataObjectTypes.Term].Update(dgvTerms.SelectedRows[0].Index, t);
                }
            }
        }

        

        private void deleteToolStripMenuItem1_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show(localizator.GetLocolizeWord("want_delete_item"), localizator.GetLocolizeWord("delete_term"), MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                if (dgvTerms.SelectedRows.Count > 0)
                {
                    string tv = Convert.ToString(lbTermVariants.SelectedItem);
                    Term t = (dataObjectsCollections[DataObjectTypes.Term][dgvTerms.SelectedRows[0].Index] as Term);
                    t.Variants.Remove(tv);
                    lbTermVariants.Items.Remove(lbTermVariants.SelectedItem);
                    dataObjectsCollections[DataObjectTypes.Term].Update(dgvTerms.SelectedRows[0].Index, t);
                }
            }
        }

        private void dgvObjects_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvObjects.SelectedRows.Count > 0)
            {
                long id = Convert.ToInt64(dgvObjects.SelectedRows[0].Cells[0].Value);
                lbProperties.Items.Clear();
                lbProperties.Items.AddRange((dataObjectsCollections[DataObjectTypes.Property].GetObjectById(id) as Property).Values.ToArray());
            }
        }

        private void addToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            string q = "";
            ObjectForm frm = new ObjectForm(dataObjectsCollections[DataObjectTypes.Theme]);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                Property p = frm.Prop;
                dataObjectsCollections[DataObjectTypes.Property].Add(p);
                Property newP = dataObjectsCollections[DataObjectTypes.Property][dataObjectsCollections[DataObjectTypes.Property].Count - 1] as Property;
                dgvObjects.Rows.Add(new object[] { newP.Id, newP.Title });
            }
        }

        
        private void modifyToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            string q = dgvObjects.SelectedRows[0].Cells[1].Value.ToString();
            if (InputBox(localizator.GetLocolizeWord("modify_new_object"), localizator.GetLocolizeWord("object"), ref q) == DialogResult.OK)
            {
                Property p = (dataObjectsCollections[DataObjectTypes.Property][dgvObjects.SelectedRows[0].Index] as Property);
                p.SetTitle(q);
                dgvObjects.SelectedRows[0].Cells[1].Value = q;
            }
        }

        private void updateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(localizator.GetLocolizeWord("are_you_sure"), localizator.GetLocolizeWord("object"), MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                if (dgvObjects.SelectedRows.Count > 0)
                {
                    dataObjectsCollections[DataObjectTypes.Property].Delete(Convert.ToInt64(dgvObjects.SelectedRows[0].Cells[0].Value));
                    dgvObjects.Rows.Remove(dgvObjects.SelectedRows[0]);
                }
            }
        }

        private void addToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (dgvObjects.SelectedRows.Count > 0)
            {
                long id = Convert.ToInt64(dgvObjects.SelectedRows[0].Cells[0].Value);
                PropertyForm frm = new PropertyForm(id);
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    Property p = dataObjectsCollections[DataObjectTypes.Property].GetObjectById(id) as Property;
                    p.addValue(frm.PValue);
                    lbProperties.Items.Add(frm.PValue);
                }
            }
        }

        private void modifyToolStripMenuItem3_Click(object sender, EventArgs e)
        {
            if (dgvObjects.SelectedRows.Count > 0)
            {
                long id = Convert.ToInt64(dgvObjects.SelectedRows[0].Cells[0].Value);
                if (lbProperties.SelectedItems.Count > 0)
                {
                    PropertyForm frm = new PropertyForm(id, lbProperties.SelectedItems[0] as PropertyValue);
                    if (frm.ShowDialog() == DialogResult.OK)
                    {
                        Property p = dataObjectsCollections[DataObjectTypes.Property].GetObjectById(id) as Property;
                        p.Values[lbProperties.SelectedIndex] = frm.PValue;
                        p.SetTitle(p.Title);
                        lbProperties.Items.Insert(lbProperties.SelectedIndex, frm.PValue);
                        lbProperties.Items.Remove(lbProperties.SelectedItems[0]);
                    }
                }
            }
        }

        private void deleteToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (dgvObjects.SelectedRows.Count > 0)
            {
                long id = Convert.ToInt64(dgvObjects.SelectedRows[0].Cells[0].Value);
                if (lbProperties.SelectedItems.Count > 0)
                {
                    if (MessageBox.Show(localizator.GetLocolizeWord("are_you_sure"), localizator.GetLocolizeWord("object"), MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        Property p = dataObjectsCollections[DataObjectTypes.Property].GetObjectById(id) as Property;
                        p.Values.Remove(lbProperties.SelectedItems[0] as PropertyValue);
                        p.SetTitle(p.Title);
                        lbProperties.Items.Remove(lbProperties.SelectedItems[0]);
                    }
                }
            }
        }

        private void dgvObjects_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvObjects.SelectedRows.Count > 0)
            {
                long id = Convert.ToInt64(dgvObjects.SelectedRows[0].Cells[0].Value);
                lbProperties.Items.Clear();
                lbProperties.Items.AddRange((dataObjectsCollections[DataObjectTypes.Property].GetObjectById(id) as Property).Values.ToArray());
            }
        }

        private void objectlistToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvQuestions.SelectedRows.Count > 0)
            {
                long id = Convert.ToInt64(dgvQuestions.SelectedRows[0].Cells[0].Value);
                Question q = dataObjectsCollections[DataObjectTypes.Question].GetObjectById(id) as Question;
                ChooseProperties frm = new ChooseProperties(q.Properties, dataObjectsCollections[DataObjectTypes.Property]);
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    q.AddProperties(frm.CurrentProperties);
                }
            }
        }


        #region ILocalizable Members

        public void Localize()
        {
            Text = localizator.GetLocolizeWord(Text);
            btnClose.Text = localizator.GetLocolizeWord(btnClose.Text);
            dgvObjects.Columns[1].HeaderText = localizator.GetLocolizeWord(dgvObjects.Columns[1].HeaderText);
            dgvQuestions.Columns[1].HeaderText = localizator.GetLocolizeWord(dgvQuestions.Columns[1].HeaderText);
            dgvTerms.Columns[1].HeaderText = localizator.GetLocolizeWord(dgvTerms.Columns[1].HeaderText);
            tbpObjects.Text = localizator.GetLocolizeWord(tbpObjects.Text);
            tbpQuestions.Text = localizator.GetLocolizeWord(tbpQuestions.Text);
            tbpTerms.Text = localizator.GetLocolizeWord(tbpTerms.Text);
            foreach (ToolStripItem i in cmsObjects.Items)
            {
                if (i is ToolStripMenuItem)
                {
                    i.Text = localizator.GetLocolizeWord(i.Text);
                }
            }

            foreach (ToolStripItem i in cmsProperties.Items)
            {
                if (i is ToolStripMenuItem)
                {
                    i.Text = localizator.GetLocolizeWord(i.Text);
                }
            }

            foreach (ToolStripItem i in cmsQuestion.Items)
            {
                if (i is ToolStripMenuItem)
                {
                    i.Text = localizator.GetLocolizeWord(i.Text);
                }
            }

            foreach (ToolStripItem i in cmsTerms.Items)
            {
                if (i is ToolStripMenuItem)
                {
                    i.Text = localizator.GetLocolizeWord(i.Text);
                }
            }

            foreach (ToolStripItem i in cmsTermVariants.Items)
            {
                if (i is ToolStripMenuItem)
                {
                    i.Text = localizator.GetLocolizeWord(i.Text);
                }
            }

        }

        #endregion
    }
}
